using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using TrabalhoSeven.Estados.Jogo;
using TrabalhoSeven.Estados.Menu;

namespace TrabalhoSeven{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game{

        GraphicsDeviceManager graphics;
        public static SpriteBatch spriteBatch;
        KeyboardState teclado_atual, teclado_anterior;
		MouseState mouse_atual, mouse_anterior;

		public static int estado_atual = GameConst.INTRO;

		Fundo cenario;
		Fundo montanhas;
		Fundo nuvens;
		Fundo arbustos;
		Vector2 aux;
		//Imagem chao;
        Personagem megaman, protoman;
		ControladorTiro cTiro;
		ControladorColisao cColisao;

		Imagem introIMG;
		double delayIntro;

		Menu menu;

		Imagem creditosIMG;

        public Game1(){
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize(){

            // TODO: Add your initialization logic here
			cTiro = new ControladorTiro();
			cColisao = new ControladorColisao();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent(){

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
			//chao = new Imagem(Content.Load<Texture2D>("chao"), new Vector2(0, 0));
			megaman = new Personagem(Content,"megaman",1);
			//protoman = new Personagem(Content, "protoman", 2);
			cenario = new Fundo(Content.Load<Texture2D>("cenario"), Window);
			montanhas = new Fundo(Content.Load<Texture2D>("montanhas"), Window);
			nuvens = new Fundo(Content.Load<Texture2D>("nuvens"), Window);
			arbustos = new Fundo(Content.Load<Texture2D>("arbustos"), Window);

			introIMG = new Imagem(Content.Load<Texture2D>("intro"), new Vector2(0,0));
			delayIntro = 0;

			menu = new Menu(Content);

			creditosIMG = new Imagem(Content.Load<Texture2D>("creditos"), new Vector2(0, 0));
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent(){

            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime){

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
			switch (estado_atual) {
				case GameConst.INTRO:
					delayIntro += gameTime.ElapsedGameTime.Milliseconds;
					if (delayIntro >= 2000) {
						estado_atual = GameConst.MENU;
					}
				break;
				case GameConst.MENU:
					mouse_atual = Mouse.GetState();
					menu.Update(mouse_atual, mouse_anterior);
					mouse_anterior = mouse_atual;
				break;
				case GameConst.JOGO:
					teclado_atual = Keyboard.GetState();
					megaman.Update(gameTime, Window, teclado_atual, teclado_anterior, cTiro);
					//protoman.Update(gameTime, Window, teclado_atual, teclado_anterior, cTiro);
					cTiro.Update(gameTime, Window);
					//cColisao.Update(megaman,protoman,cTiro);
					if (megaman.Direita == true) {
						if (megaman.Posicao.X > Window.ClientBounds.Width * 2 / 3) {
							aux = new Vector2(Window.ClientBounds.Width * 2 / 3, megaman.Posicao.Y);
							megaman.Posicao = aux;
						}
					}
					else {
						if (megaman.Posicao.X < Window.ClientBounds.Width * 1 / 3) {
							aux = new Vector2(Window.ClientBounds.Width * 1 / 3, megaman.Posicao.Y);
							megaman.Posicao = aux;
						}
					}
					if (megaman.Movendo) {
						aux = new Vector2(megaman.Velocidade.X, 0);
						cenario.Update(gameTime, aux, megaman.Direita);
						montanhas.Update(gameTime, aux, megaman.Direita);
						nuvens.Update(gameTime, aux, megaman.Direita);
						arbustos.Update(gameTime, aux, megaman.Direita);
					}

					teclado_anterior = teclado_atual;
				break;
				case GameConst.CREDITOS:
					teclado_atual = Keyboard.GetState();
					if ((teclado_atual.IsKeyDown(Keys.Enter) == true) && (teclado_anterior.IsKeyDown(Keys.Enter) == false)){
						estado_atual = GameConst.MENU;	
					}
					teclado_anterior = teclado_atual;
				break;
				case GameConst.SAIR:
					this.Exit();
				break;
			}

			

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime){

            GraphicsDevice.Clear(Color.CornflowerBlue);

			// TODO: Add your drawing code here
			spriteBatch.Begin();

			switch (estado_atual) {
				case GameConst.INTRO:
					IsMouseVisible = true;
					introIMG.Draw(spriteBatch);
				break;
				case GameConst.MENU:
					IsMouseVisible = true;
					menu.Draw(spriteBatch);
				break;
				case GameConst.CREDITOS:
					IsMouseVisible = true;
					creditosIMG.Draw(spriteBatch);
				break;
				case GameConst.JOGO:
					IsMouseVisible = false;
					//chao.Draw(spriteBatch);
					nuvens.Draw(gameTime, spriteBatch);
					montanhas.Draw(gameTime, spriteBatch);
					cenario.Draw(gameTime, spriteBatch); 
					megaman.Draw(gameTime, spriteBatch);
					//protoman.Draw(gameTime, spriteBatch);
					cTiro.Draw(gameTime, spriteBatch);
					arbustos.Draw(gameTime, spriteBatch);
				break;
			}

			

			spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
